package cn.shop.merchant.controller.media;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.model.media.Notice;
import cn.shop.merchant.req.MediaQueryReq;
import cn.shop.merchant.service.NoticeService;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * 公告接口
 *
 * @author : 李一涵
 * @date : Created in 2021-11-4 11:02
 * @description : 公告controller
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    /**
     * 获取公告列表
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("")
    public ApiResult<Map<String, Object>> list(@Validated MediaQueryReq req) {
        long uid = StpUtil.getSession().getLong(UID);
        Map<String, Object> data = noticeService.list(uid, req);
        return ApiResult.success(data);
    }

    /**
     * 保存公告
     *
     * @param notice 公告
     * @return 返回结果
     */
    @PostMapping("")
    @OperationLog(desc = "新增公告", type = OperationType.CREATE)
    public ApiResult<Map<String, Object>> insert(@Validated @RequestBody Notice notice) {
        if (notice == null) {
            throw new RuntimeException("公告对象为空");
        }
        boolean isSuccess = noticeService.save(notice);
        return ApiResult.result(isSuccess);
    }

    /**
     * 更新公告
     *
     * @param notice 公告
     * @return 返回结果
     */
    @PutMapping("")
    @OperationLog(desc = "修改公告", type = OperationType.UPDATE)
    public ApiResult<Map<String, Object>> update(@RequestBody Notice notice) {
        long uid = StpUtil.getSession().getLong(UID);
        boolean isSuccess = noticeService.update(notice, uid);
        return ApiResult.result(isSuccess);
    }

    /**
     * 删除公告
     *
     * @param ids 编号
     * @return 返回结果
     */
    @PostMapping("/delete")
    @OperationLog(desc = "删除公告", type = OperationType.DELETE)
    public ApiResult<Map<String, Object>> delete(@RequestBody List<Long> ids) {
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("请检查登录状态");
        }
        boolean isSuccess = noticeService.delete(ids, uid);
        return ApiResult.result(isSuccess);
    }

    /**
     * 获取公告信息
     *
     * @param id 编号
     * @return 返回结果
     */
    @GetMapping("/{id}")
    public ApiResult<Map<String, Object>> info(@PathVariable("id") long id) {
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("请检查登录状态");
        }
        Map<String, Object> data = noticeService.info(id, uid);
        return ApiResult.success(data);
    }
}
